package com.maiji.cloud.controller;

import com.maiji.cloud.entities.login.Label;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.login.*;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.login.*;
import com.maiji.cloud.response.shopingmall.GoodsResData;
import com.maiji.cloud.service.*;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/deit/")
public class DietController {
	private static final Logger logger = LoggerFactory.getLogger(DietController.class);

	@Autowired
	private DietService dietService;
	@Autowired
    private FoodMaterialTypeLableService foodMaterialTypeLableService;
    @Autowired
    private FoodMaterialService foodMaterialService;

	/**
	 * 添加饮食记录
	 * 
	 * @return
	 */
	@PostMapping("addDietLog")
	public BaseResDto addDietLog(@RequestBody AddDietLogResDto addDietLogResDto) throws Exception{
		logger.info("DietController.addDietLog,addDietLogResDto is {}", JSON.toJSONString(addDietLogResDto));

		return dietService.addDietLog(addDietLogResDto);
	}
	
	/**
	 * 新版本添加饮食记录
	 * 
	 * @return
	 */
	@PostMapping("newVersionAddDietLog")
	public BaseResDto newVersionAddDietLog(@RequestBody AddDietLogResDto addDietLogResDto) throws Exception{
		logger.info("DietController.newVersionAddDietLog,addDietLogResDto is {}", JSON.toJSONString(addDietLogResDto));

		return dietService.newVersionAddDietLog(addDietLogResDto);
	}

	/**
	 * 根据食材名字搜索
	 * 
	 * @return
	 */
	@PostMapping("searchFoodMaterialByName")
	public FoodTypeLableResDto searchFoodMaterialByName(@RequestBody BaseDataReqDto<String> param) {
		logger.info("DietController.searchFoodMaterialByName,param is {}", JSON.toJSONString(param));
		if(StringUtils.isBlank(param.getData())) return new FoodTypeLableResDto(Status.PARAMETERERROR);
		return dietService.searchFoodMaterialByName(param);
	}
	
	/**
	 * 获取饮食记录
	 * 
	 * @param getDietLogReqDto
	 * @return
	 * @throws Exception
	 */
	@PostMapping("getDietLog")
	public DietLogResDto getDietLog(@RequestBody GetDietLogReqDto getDietLogReqDto) throws Exception {
		logger.info("DietController.getDietLog,getDietLogReqDto is {}", JSON.toJSONString(getDietLogReqDto));

		return dietService.getDietLog(getDietLogReqDto);
	}
	
	/**
	 * 食材搜索
	 * 
	 * @return
	 */
	@PostMapping("searchFoodMaterial")
	public FoodTypeLableResDto searchFoodMaterial(@RequestBody SearchFoodMaterialReqDto searchFoodMaterialReqDto) {
		logger.info("DietController.searchFoodMaterial,searchFoodMaterialReqDto is {}", JSON.toJSONString(searchFoodMaterialReqDto));
        String typeId = searchFoodMaterialReqDto.getData().getTypeId();
        String laybleId = searchFoodMaterialReqDto.getData().getLaybleId();
        String name = searchFoodMaterialReqDto.getData().getName();
        if (StringUtil.isBlank(typeId)) return new FoodTypeLableResDto(Status.PARAMETERERROR);
        return dietService.searchFoodMaterial(typeId, laybleId, name);
	}

	/**
	 * 获取某一段时间的饮食记录
	 * 
	 * @param getDietLogReqDto
	 * @return
	 * @throws Exception
	 */
	@PostMapping("getDietLogDaysList")
	public BaseDataResDto<List<DietLogDaysListResData>> getDietLogDaysList(@RequestBody GetDietLogReqDto getDietLogReqDto) throws Exception {
		logger.info("DietController.getDietLog,getDietLogReqDto is {}", JSON.toJSONString(getDietLogReqDto));
		
		return dietService.getDietLogDaysList(getDietLogReqDto);
	}
	
	/**
	 * 获取某一段时间的饮食记录
	 * 
	 * @param getDietLogReqDto
	 * @return
	 * @throws Exception
	 */
	@PostMapping("newVersionGetDietLogDaysList")
	public BaseDataResDto<List<DietLogDaysListResData>> newVersionGetDietLogDaysList(@RequestBody GetDietLogReqDto getDietLogReqDto) throws Exception {
		logger.info("DietController.getDietLog,getDietLogReqDto is {}", JSON.toJSONString(getDietLogReqDto));
		
		return dietService.newVersionGetDietLogDaysList(getDietLogReqDto);
	}

    /**
     *获取所有的一级食材列表
     * @return
     */
	@PostMapping("getFoodmaterialTypeLable")
	public FoodTypeLableResDto getFoodmaterialTypeLable(){
		return dietService.getFoodmaterialTypeLable();
	}

    /**
     * 添加食材
     * @return
     */
	@PostMapping("addFood")
	public BaseResDto addFood(@RequestBody BaseDataReqDto<FoodReqData> baseDataReqDto){
        try {
            FoodReqData data = baseDataReqDto.getData();
            if (data == null) return new BaseResDto(Status.PARAMETERERROR);
            return foodMaterialService.addFood(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

    /**
     *  编辑食材
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("editFood")
    public BaseResDto editFood(@RequestBody BaseDataReqDto<FoodReqData> baseDataReqDto) {
        try {
            FoodReqData data = baseDataReqDto.getData();
            if (data == null) return new BaseResDto(Status.PARAMETERERROR);
            return foodMaterialService.editFood(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

    /**
     * 获得食材列表_PC
     * @return
     */
	@PostMapping("findAllFoods")
	public BaseMetaResDto<List<FoodMaterialResData>> findAllFoods(@RequestBody BaseDataReqDto<FoodFindAllReqData> baseDataReqDto){
        FoodFindAllReqData data = baseDataReqDto.getData();
        ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
        if (data == null) return new BaseMetaResDto<>(Status.ERROR);
        return foodMaterialService.findAllFoods(data, reqMetaData);
	}
	
	/**
	 * 获取食材详情
	 * @return
	 */
	@PostMapping("getFoodMaterialDetailById")
	public FoodMaterialDetialResDto getFoodMaterialDetailById(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
		logger.info("DietController.getFoodMaterialDetailById,param is {}", JSON.toJSONString(baseDataReqDto));
        String foodId = baseDataReqDto.getData();
        return foodMaterialService.getFoodMaterialDetailById(foodId);
	}

	/**
	 * 批量逻辑删除食材
	 * @return
	 */
	@PostMapping("delFoodsByIds")
	public BaseResDto delFoodsByIds(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto) {
        List<String> foodIds = baseDataReqDto.getData();
        if (foodIds == null || foodIds.size() == 0) return new BaseResDto(Status.PARAMETERERROR);
        return foodMaterialService.delFoodsByIds(foodIds);
	}

    /**
     * 获取用户收藏食材列表
     * @param baseReqMetaDto
     * @param token
     * @return
     */
    @PostMapping("foodMaterialCollectionList")
    public  BaseDataResDto<List<FoodMaterialResData>> foodMaterialCollectionList(@RequestBody BaseDataReqDto baseReqMetaDto,
                                                                   @RequestHeader("maijiToken") String token) {
        ReqMetaData reqMetaData = baseReqMetaDto.getMetaData();
        if (reqMetaData == null) return new  BaseDataResDto(Status.PARAMETERERROR);
        return dietService.foodMaterialCollectionList(token, reqMetaData);
    }

    /**
     * 食材收藏/取消收藏
     * @param param
     * @param token
     * @return
     */
    @PostMapping("thumbsfoodMaterial")
    public BaseResDto thumbsfoodMaterial(@RequestBody ThumbsfoodMaterialReqDto param, @RequestHeader("maijiToken") String token){
        logger.info("DietController.thumbsfoodMaterial,param is {},token is {}",
                JSON.toJSONString(param),token);


        return dietService.thumbsfoodMaterial(token, param);

    }
    
    /**
     * 批量删除用户收藏食材（空参为清空收藏）
     * @param baseDataReqDto 食材id集合
     * @param token
     * @return
     */
    @PostMapping("cancelCollectionFoodMaterial")
    public BaseResDto cancelCollectionFoodMaterial(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto,
                                              @RequestHeader("maijiToken") String token) {
        List<String> foodMaterialCollectionids = baseDataReqDto.getData();
        return dietService.cancelCollectionFoodMaterial(foodMaterialCollectionids, token);
    }
    
    /**
     *  根据食材ID获取食材的标签列表
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllLabelsByFoodId")
    public BaseDataResDto<List<Label>> findAllLabelsByFoodId(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String foodId = baseDataReqDto.getData();
        if (StringUtil.isBlank(foodId)) return new BaseDataResDto(Status.PARAMETERERROR);
        return foodMaterialService.findAllLabelsByFoodId(foodId);
    }

    /**
     *  获取食材分类分级列表
     * @return
     */
    @PostMapping("findAllFoodTypes")
    public BaseDataResDto<List<FoodTypeResData>> findAllFoodTypes(@RequestBody BaseDataReqDto<Boolean> baseDataReqDto) {
        Boolean isFindFoods = baseDataReqDto.getData();
        if (isFindFoods == null) isFindFoods = false;
        return foodMaterialTypeLableService.findAllFoodTypes(isFindFoods);
    }

    /**
     *  编辑食材分类分级
     * @return
     */
    @PostMapping("editFoodType")
    public BaseResDto editFoodType(@RequestBody BaseDataReqDto<FoodTypeResData> baseDataReqDto) {
        FoodTypeResData foodType = baseDataReqDto.getData();
        return foodMaterialTypeLableService.editFoodType(foodType);
    }

    /**
     *  获取食材相关商品列表
     *  根据食材ID获取食材相关商品列表
     * @return
     */
    @PostMapping("findAllGoodsByFoodId")
    public BaseMetaResDto<List<GoodsResData>> findAllGoodsByFoodId(@RequestBody BaseDataReqDto<String> baseDataReqDto){
        String foodId = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        if (StringUtil.isBlank(foodId)) return new BaseMetaResDto<>(Status.ERROR);
        return foodMaterialService.findAllGoodsByFoodIds(Arrays.asList(foodId), metaData);
    }

    /**
     *  批量导入食材
     * @param uploadFile
     * @return
     */
    @PostMapping("uploadFoods")
    public BaseDataResDto<String> uploadFoods(@RequestBody MultipartFile uploadFile) {
        try {
            if (uploadFile == null) return new BaseDataResDto<>(Status.ERROR);
            return foodMaterialService.uploadFoods(uploadFile);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseDataResDto<>(Status.ERROR);
        }
    }

    /**
     * 根据饮食记录id集合批量删除
     * @param param
     * @return
     */
    @PostMapping("deleteDielogById")
    public BaseResDto deleteDielogById(@RequestBody BaseDataReqDto<List<String>> param){
    	if(param.getData() == null || param.getData().size() < 1) return new BaseResDto(Status.PARAMETERERROR);
    	return dietService.deleteDielogById(param.getData());
    }
    
}
